/**
 * 返回顶部js
 * @version 1.2
 * @author yulipu
 * 用法 
 * new YBackToTop('mybtn').init();
 */
function YBackToTop(btnId) {
	this.backTopId = 0;
	this.btn = document.getElementById(btnId);
	this.doc = document;
	//this.init();
}
YBackToTop.prototype = {
	constructor : YBackToTop,
	E : {
		addHandler : function(ele, type, func) {
			if(ele.addEventListener) {
				ele.addEventListener(type, func, false);
			} else if(ele.attachEvent) {
				ele.attachEvent("on" + type, func);	
			} else {
				ele["on" + type] = func;	
			}
		},	
		removeHandler : function(ele, type, func) {
			if(ele.removeEventListener) {
				ele.removeEventListener(type, func, false);
			}	else if(ele.detachEvent) {
				ele.detachEvent("on" + type, func);	
			} else {
				ele["on" + type] = null;	
			}
		}
	},
	
	init : function() {
		var _this = this,
			version = 0;
		if (!!window.ActiveXObject) {
			version = parseInt(navigator.userAgent.toLowerCase().match(/msie (\d+)/)[1]);
		}
		this.E.addHandler(this.btn, "click", function() {
			_this.backTopId = setInterval(function(){
				_this.backTop();
			}, 30);
		});
		this.E.addHandler(window, "scroll", function() {
			var scrTop = _this.doc.documentElement.scrollTop || _this.doc.body.scrollTop;

			_this.btn.style.display = scrTop >= 100 ? "block" : "none";
			//ie6下返回顶端位置
			if(version == 6) {
				_this.btn.style.position = 'absolute';
				_this.btn.style.top = 300 + scrTop + "px";
			}
		});
	},
  
	backTop:function() {
		var scrTop = this.doc.documentElement.scrollTop || this.doc.body.scrollTop,
			speed = Math.ceil(scrTop / 4);
		if (scrTop == 0)
			clearInterval(this.backTopId);
		else
			this.doc.documentElement.scrollTop = this.doc.body.scrollTop = scrTop - speed;
	}
};